package com.higgins.eventtracking.client.health.components;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.EventTrackingService;
import com.higgins.eventtracking.client.EventTrackingServiceAsync;
import com.higgins.eventtracking.client.dto.HealthAlertDTO;
import com.higgins.eventtracking.client.health.PopupQuestionBox;
import com.higgins.eventtracking.client.health.fundamentals.Fundamentals;
import com.higgins.eventtracking.server.dataModel.HealthAlert;

public class GenericStatsTab extends VerticalPanel implements GenericTabInterface	{
	private final EventTrackingServiceAsync greetingService = GWT
										.create(EventTrackingService.class);
	private boolean firstRun = true;
	private Boolean alert1 = null;
	private Boolean alert2 = null;
	private Boolean alert3 = null;
	private Boolean alert4 = null;
	private Boolean alert5 = null;
	private Boolean alert6 = null;
	private Boolean alert7 = null;
	private Boolean alert8 = null;
	private GenericStatsTabParent parentTab = null;
	private String tabTitle = null;
	private Integer tabIndex = null;
	private List<HealthAlertDTO> healthAlertDTOs = null;
	private int maxColumn = 0;
	private int maxRow = 0;
	private List<List<HealthAlertDTO>> dtoRows = new ArrayList<List<HealthAlertDTO>>();
	private List<List<Label>> labelRows = new ArrayList<List<Label>>();
	private Integer width = 175;
	private Integer height = 20;
	private GenericChartPanel chartPanel = null;

	
	public GenericStatsTab(GenericStatsTabParent parentTab, String tabTitle, Integer width)	{
		this.parentTab = parentTab;
		this.tabTitle = tabTitle;
		this.width = width;
		setHeight("200px");
		setWidth("800px");
		setSpacing(0);
		setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
	}
	public String getTabTitle()	{
		return tabTitle;
	}
	public void setTabTitle(String tabTitle)	{
		this.tabTitle = tabTitle;
	}
	public void update()	{
		if (firstRun)	{
			greetingService.retrieveHealthAlerts(tabTitle, new AsyncCallback<List<HealthAlertDTO>>()  {
				@Override
				public void onFailure(Throwable caught) {
					System.out.println(caught.toString());
				}

				@Override
				public void onSuccess(List<HealthAlertDTO> results) {
					healthAlertDTOs = results;
					initialise();
				}  });
		}
	}
	public void initialise()	{
		organiseAlerts();
		setLabelWidths();
//		addFields();
		populateFields();
		populateChartPanel();
		firstRun = false;
	}
	public void populateChartPanel()	{
		if (chartPanel!=null)	{
			add(chartPanel);
		}
	}

	public void organiseAlerts()	{
		int columnIndex = 0;
		int rowIndex = 0;
		List<HealthAlertDTO> dtoRow = new ArrayList<HealthAlertDTO>();
		for (HealthAlertDTO dto:healthAlertDTOs)	{
			if (rowIndex!=dto.getRow())	{
				dtoRows.add(dtoRow);
				dtoRow = new ArrayList<HealthAlertDTO>();
			} 
			dtoRow.add(dto);
			rowIndex = dto.getRow();
			columnIndex = dto.getColumn();
			if (rowIndex>maxRow)	{
				maxRow = rowIndex;
			}
			if (columnIndex > maxColumn)	{
				maxColumn = columnIndex;
			}
		}
		if (dtoRow.size()>0)	{
			dtoRows.add(dtoRow);
		}
	}

	private void setLabelWidths()	{
		List<Label> labelRow = new ArrayList<Label>();
		if (maxRow<3)	{
			maxRow = 3;
		}
		for (int rowIndex = 0; rowIndex <= maxRow; rowIndex++)	{
			HorizontalPanel rowPanel = new HorizontalPanel();
			rowPanel.setSpacing(0);
			for (int columnIndex = 0;columnIndex <= maxColumn; columnIndex++)	{
				Label label = new Label();
				label.setWidth(width+"px");
				//
				label.setHeight(height + "px");
				labelRow.add(label);

				rowPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
				rowPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
				rowPanel.setHeight(height+"px");
				rowPanel.add(label);
				rowPanel.setCellHeight(label, "0px");
			}
			add(rowPanel);
			labelRows.add(labelRow);
			labelRow = new ArrayList<Label>();
		}
	}

	private void populateFields()	{
		for (HealthAlertDTO dto:healthAlertDTOs)	{
			populateAlert(dto);
		}
	}
	
	private void populateAlert(HealthAlertDTO dto)	{
		greetingService.retrievePeriodSinceAlert(dto,
				new AsyncCallback<HealthAlertDTO>() {
					public void onFailure(Throwable caught) {
						alert1 = false;
					}
					public void onSuccess(final HealthAlertDTO dto) {
						int columnIndex = dto.getColumn();
						int rowIndex = dto.getRow();
						List<Label> labelRow = labelRows.get(rowIndex);
						Label label = labelRow.get(columnIndex);
						if (dto.getCount())		{
							label.setText(dto.getNumberOfPeriodsToDate() + " " + dto.getName());
						} else {
							label.setText(dto.getNumberOfPeriodsToDate() + " " + dto.getTimePeriod() + " since last " + dto.getEventName());
						}
						if (dto.getNumberOfPeriodsToDate()!=null && dto.getNumberOfPeriodsToDate() > dto.getEvery())	{
							label.addStyleName("highlightAlertLabel");
							parentTab.updateTabTitle(tabIndex, tabTitle, true);
							alert1 = true;
						} else {
							alert1 = false;
						}
					}
				});
	}
	
//	private void passBackToParent()	{
//		if (alert1!=null && alert2!=null && alert3!=null && alert4!=null && alert5!=null && alert6!=null && alert7!=null && alert8!=null)	{
//			//parentTab.addDigestionTab(alert1||alert2||alert3||alert4||alert5||alert6||alert7||alert8);
//			parentTab.updateTabTitle(tabIndex, tabTitle + "(DONE)", false);
//		}
//	}
	@Override
	public Integer getTabIndex() {
		return tabIndex;
	}
	@Override
	public void setTabIndex(Integer index) {
		this.tabIndex = index;
	}
	@Override
	public void addChartPanel(GenericChartPanel chartPanel) {
		this.chartPanel = chartPanel;
	}
}
